/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SMART_SERVE_SERVICES_SERVER_SESSION_MANAGER_H
#define SMART_SERVE_SERVICES_SERVER_SESSION_MANAGER_H

#include "session/creator.h"
#include "session/reuser/reuser.h"
#include "session/reuser/simple.h"
#include "session/session.h"
#include "task.h"
#include <map>

namespace OHOS {
namespace SmartServe {

// 访问该类任意成员前置条件：已获取 Service::msMgmtMutex_
class SessionManager {
public:
    SessionManager() : reuser_(new SimpleReuser())
    {}

    // 管理

    Session* GetOrCreateSession(EvalTask* task);
    Session* GetOrCreateSession(EvalLLMTask* task);
    std::unique_ptr<Session> RemoveSession(Session* session);

    std::multimap<std::string, std::unique_ptr<Session>> allSessions_;

    // 注册

    bool Register(std::unique_ptr<SessionCreator> creator);
    bool UnRegister(const std::string& name)
    {
        return registry_.erase(name);
    }

    SessionCreator* GetCreator(const std::string& name)
    {
        auto it = registry_.find(name);
        return (it != registry_.end()) ? it->second.get() : nullptr;
    }
    const std::map<std::string, std::unique_ptr<SessionCreator>>& GetRegistry() const
    { // 外部加msMgmtMutex_锁
        return registry_;
    }

private:
    std::unique_ptr<Reuser> reuser_;
    std::map<std::string, std::unique_ptr<SessionCreator>> registry_;
};

} // namespace SmartServe
} // namespace OHOS

#endif // SMART_SERVE_SERVICES_SERVER_SESSION_MANAGER_H
